#include "etcd.hpp"
#include "channel.hpp"
#include <gflags/gflags.h>
#include <thread>
#include <gtest/gtest.h>
#include "utils.hpp"
#include "user.pb.h"
#include "base.pb.h"

DEFINE_bool(run_mode, false, "程序的运行模式，false-调试； true-发布；");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

DEFINE_string(etcd_host, "http://127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(user_service, "/service/user_service", "服务监控根目录");

zrb::ServiceManager::ptr user_channel;

zrb::UserInfo user_info;

std::string login_ssid;

std::string new_nickname = "猪妈妈";

// TEST(用户子服务测试,用户注册)
// {
//     std::cout << "正常运行333" << std::endl;

//     zrb::ServiceChannel::ChannelPtr channel;
//     try 
//     {
//         //3. 通过Rpc信道管理对象，获取提供Echo服务的信道
//         channel = user_channel->choose(FLAGS_user_service);
//         std::cout << "正常运行444" << std::endl;
//     }
//     catch(std::exception e)
//     {
//         LOG_ERROR("出现错误 {}",e.what());
//     }
    
//     if (!channel) {
//         std::this_thread::sleep_for(std::chrono::seconds(1));
//         return;
//     }
//     // std::cout << "正常运行444" << std::endl;
    
//     //4. 发起EchoRpc调用（通过rpc信道）
//     zrb::UserService_Stub stub(channel.get());
//     zrb::UserRegisterReq req;
//     req.set_request_id(zrb::uuid());
//     req.set_nickname("猪爷爷");
//     req.set_password("2825280463");

//     std::cout << "正常运行555" << std::endl;

//     brpc::Controller cntl;
//     zrb::UserRegisterRsp rsp;
//     stub.UserRegister(&cntl, &req, &rsp, nullptr);

//     std::cout << "正常运行666" << std::endl;

//     //3.检测返回值中上传是否成功
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(rsp.success()); //必须为真
// }

// TEST(用户子服务测试, 用户登录测试) {
//     auto channel = user_channel->choose(FLAGS_user_service);//获取通信信道
//     ASSERT_TRUE(channel);

//     zrb::UserLoginReq req;
//     req.set_request_id(zrb::uuid());
//     req.set_nickname("小猪乔治");
//     req.set_password("2825233363");
//     zrb::UserLoginRsp rsp;
//     brpc::Controller cntl;
//     zrb::UserService_Stub stub(channel.get());
//     stub.UserLogin(&cntl, &req, &rsp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(rsp.success());
//     login_ssid = rsp.login_session_id();
// }
// TEST(用户子服务测试, 用户头像设置测试) {
//     auto channel = user_channel->choose(FLAGS_user_service);//获取通信信道
//     ASSERT_TRUE(channel);

//     zrb::SetUserAvatarReq req;
//     req.set_request_id(zrb::uuid());
//     req.set_user_id(user_info.user_id());
//     req.set_session_id(login_ssid);
//     req.set_avatar(user_info.avatar());
//     zrb::SetUserAvatarRsp rsp;
//     brpc::Controller cntl;
//     zrb::UserService_Stub stub(channel.get());
//     stub.SetUserAvatar(&cntl, &req, &rsp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(rsp.success());
// }
// TEST(用户子服务测试, 用户签名设置测试) {
//     auto channel = user_channel->choose(FLAGS_user_service);//获取通信信道
//     ASSERT_TRUE(channel);

//     zrb::SetUserDescriptionReq req;
//     req.set_request_id(zrb::uuid());
//     req.set_user_id(user_info.user_id());
//     req.set_session_id(login_ssid);
//     req.set_description(user_info.description());
//     zrb::SetUserDescriptionRsp rsp;
//     brpc::Controller cntl;
//     zrb::UserService_Stub stub(channel.get());
//     stub.SetUserDescription(&cntl, &req, &rsp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(rsp.success());
// }
// TEST(用户子服务测试, 用户昵称设置测试) {
//     auto channel = user_channel->choose(FLAGS_user_service);//获取通信信道
//     ASSERT_TRUE(channel);

//     zrb::SetUserNicknameReq req;
//     req.set_request_id(zrb::uuid());
//     req.set_user_id(user_info.user_id());
//     req.set_session_id(login_ssid);
//     req.set_nickname(new_nickname);
//     zrb::SetUserNicknameRsp rsp;
//     brpc::Controller cntl;
//     zrb::UserService_Stub stub(channel.get());
//     stub.SetUserNickname(&cntl, &req, &rsp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(rsp.success());
// }


// TEST(用户子服务测试, 用户信息获取测试) {
//     auto channel = user_channel->choose(FLAGS_user_service);//获取通信信道
//     ASSERT_TRUE(channel);

//     zrb::GetUserInfoReq req;
//     req.set_request_id(zrb::uuid());
//     req.set_user_id(user_info.user_id());
//     req.set_session_id(login_ssid);
//     zrb::GetUserInfoRsp rsp;
//     brpc::Controller cntl;
//     zrb::UserService_Stub stub(channel.get());
//     stub.GetUserInfo(&cntl, &req, &rsp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(rsp.success());
//     ASSERT_EQ(user_info.user_id(), rsp.user_info().user_id());
//     ASSERT_EQ(new_nickname, rsp.user_info().nickname());
//     ASSERT_EQ(user_info.description(), rsp.user_info().description());
//     ASSERT_EQ("", rsp.user_info().phone());
//     ASSERT_EQ(user_info.avatar(), rsp.user_info().avatar());
// }

void set_user_avatar(const std::string &uid, const std::string &avatar) {
    auto channel = user_channel->choose(FLAGS_user_service);//获取通信信道
    ASSERT_TRUE(channel);
    zrb::SetUserAvatarReq req;
    req.set_request_id(zrb::uuid());
    req.set_user_id(uid);
    req.set_session_id(login_ssid);
    req.set_avatar(avatar);
    zrb::SetUserAvatarRsp rsp;
    brpc::Controller cntl;
    zrb::UserService_Stub stub(channel.get());
    stub.SetUserAvatar(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

// TEST(用户子服务测试, 批量用户信息获取测试) {
//     // set_user_avatar("userid", "小猪佩奇的头像数据"); //将头像数据存入文件系统
//     // set_user_avatar("userid1", "小猪乔治的头像数据");
//     // set_user_avatar("0996-2792d4c5-0000", "猪妈妈头像数据");

//     auto channel = user_channel->choose(FLAGS_user_service);//获取通信信道
//     ASSERT_TRUE(channel);

//     zrb::GetMultiUserInfoReq req;
//     req.set_request_id(zrb::uuid());
//     req.add_users_id("0996-2792d4c5-0000"); 
//     req.add_users_id("03b6-4c692527-0000");
//     req.add_users_id("userid");
//     req.add_users_id("userid1");

//     zrb::GetMultiUserInfoRsp rsp;
//     brpc::Controller cntl;
//     zrb::UserService_Stub stub(channel.get());
//     stub.GetMultiUserInfo(&cntl, &req, &rsp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(rsp.success());

//     auto users_map = rsp.mutable_users_info();
    
//     zrb::UserInfo fuser = (*users_map)["0996-2792d4c5-0000"];
//     ASSERT_EQ(fuser.user_id(), "0996-2792d4c5-0000");
//     ASSERT_EQ(fuser.nickname(), "猪妈妈");
//     ASSERT_EQ(fuser.description(), "这是一个帅气的猪爸爸");
//     ASSERT_EQ(fuser.phone(), "");
//     ASSERT_EQ(fuser.avatar(), "猪妈妈头像数据");

//     zrb::UserInfo puser = (*users_map)["03b6-4c692527-0000"];
//     ASSERT_EQ(puser.user_id(), "03b6-4c692527-0000");
//     ASSERT_EQ(puser.nickname(), "猪爸爸");
//     ASSERT_EQ(puser.description(), "");
//     ASSERT_EQ(puser.phone(), "");
//     ASSERT_EQ(puser.avatar(), "");
    
//     zrb::UserInfo quser = (*users_map)["userid"];
//     ASSERT_EQ(quser.user_id(), "userid");
//     ASSERT_EQ(quser.nickname(), "小猪佩奇");
//     ASSERT_EQ(quser.description(), "3333");
//     ASSERT_EQ(quser.phone(), "222");
//     ASSERT_EQ(quser.avatar(), "小猪佩奇的头像数据");

//     zrb::UserInfo tuser = (*users_map)["userid1"];
//     ASSERT_EQ(tuser.user_id(), "userid1");
//     ASSERT_EQ(tuser.nickname(), "小猪乔治");
//     ASSERT_EQ(tuser.description(), "3113");
//     ASSERT_EQ(tuser.phone(), "22233333");
//     ASSERT_EQ(tuser.avatar(), "小猪乔治的头像数据");
// }

std::string code_id;
void get_code() {
    auto channel = user_channel->choose(FLAGS_user_service);//获取通信信道
    ASSERT_TRUE(channel);

    zrb::PhoneVerifyCodeReq req;
    req.set_request_id(zrb::uuid());
    req.set_phone_number(user_info.phone());
    zrb::PhoneVerifyCodeRsp rsp;
    brpc::Controller cntl;
    zrb::UserService_Stub stub(channel.get());
    stub.GetPhoneVerifyCode(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    code_id = rsp.verify_code_id();
}


// TEST(用户子服务测试, 手机号注册) {
//     get_code();
//     auto channel = user_channel->choose(FLAGS_user_service);//获取通信信道
//     ASSERT_TRUE(channel);

//     zrb::PhoneRegisterReq req;
//     req.set_request_id(zrb::uuid());
//     req.set_phone_number(user_info.phone());
//     req.set_verify_code_id(code_id);
//     std::cout << "手机号注册，输入验证码：" << std::endl;
//     std::string code;
//     std::cin >> code;
//     req.set_verify_code(code);
//     zrb::PhoneRegisterRsp rsp;
//     brpc::Controller cntl;
//     zrb::UserService_Stub stub(channel.get());
//     stub.PhoneRegister(&cntl, &req, &rsp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(rsp.success());
// }

// TEST(用户子服务测试, 手机号登录) {
//     std::this_thread::sleep_for(std::chrono::seconds(3));
//     get_code();
//     auto channel = user_channel->choose(FLAGS_user_service);//获取通信信道
//     ASSERT_TRUE(channel);

//     zrb::PhoneLoginReq req;
//     req.set_request_id(zrb::uuid());
//     req.set_phone_number(user_info.phone());
//     req.set_verify_code_id(code_id);
//     std::cout << "手机号登录，输入验证码：" << std::endl;
//     std::string code;
//     std::cin >> code;
//     req.set_verify_code(code);
//     zrb::PhoneLoginRsp rsp;
//     brpc::Controller cntl;
//     zrb::UserService_Stub stub(channel.get());
//     stub.PhoneLogin(&cntl, &req, &rsp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(rsp.success());
//     std::cout << "手机登录会话ID：" << rsp.login_session_id() << std::endl;
// }

// TEST(用户子服务测试, 手机号设置) {
//     // std::this_thread::sleep_for(std::chrono::seconds(10));
//     get_code();
//     auto channel = user_channel->choose(FLAGS_user_service);//获取通信信道
//     ASSERT_TRUE(channel);

//     zrb::SetUserPhoneNumberReq req;
//     req.set_request_id(zrb::uuid());
//     std::cout << "手机号设置时，输入用户ID：" << std::endl;
//     std::string user_id;
//     std::cin >> user_id;
//     req.set_user_id(user_id);
//     req.set_phone_number("15745414456");
//     req.set_phone_verify_code_id(code_id);
//     std::cout << "手机号设置时，输入验证码：" << std::endl;
//     std::string code;
//     std::cin >> code;
//     req.set_phone_verify_code(code);
//     zrb::SetUserPhoneNumberRsp rsp;
//     brpc::Controller cntl;
//     zrb::UserService_Stub stub(channel.get());
//     stub.SetUserPhoneNumber(&cntl, &req, &rsp, nullptr);
//     ASSERT_FALSE(cntl.Failed());
//     ASSERT_TRUE(rsp.success());
// }

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    google::ParseCommandLineFlags(&argc, &argv, true);
    zrb::init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);
    std::cout << "正常运行111" << std::endl;
    //1. 先构造Rpc信道管理对象
    user_channel = std::make_shared<zrb::ServiceManager>();
    user_channel->declared(FLAGS_user_service);
    auto put_cb = std::bind(&zrb::ServiceManager::onServiceOnline, user_channel.get(), std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&zrb::ServiceManager::onServiceOffline, user_channel.get(), std::placeholders::_1, std::placeholders::_2);
    //2. 构造服务发现对象
    zrb::Discovery::ptr dclient = std::make_shared<zrb::Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);
    std::cout << "正常运行222" << std::endl;

    // user_info.set_nickname("猪妈妈");
    // user_info.set_user_id("0996-2792d4c5-0000");
    // user_info.set_description("这是一个帅气的猪爸爸");
    // user_info.set_phone("15360571912");
    // user_info.set_avatar("猪妈妈头像数据");
    // testing::InitGoogleTest(&argc, argv);
    // LOG_DEBUG("开始测试！");

    set_user_avatar("userid", "小猪佩奇的头像数据"); //将头像数据存入文件系统
    set_user_avatar("userid1", "小猪乔治的头像数据");
    set_user_avatar("0996-2792d4c5-0000", "猪妈妈头像数据");

    return RUN_ALL_TESTS();;
}

